# -*- coding: utf-8 -*-
#from __future__ import unicode_literalsfrom django.shortcuts import render

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from live_api import pili
from live_api.tools.Output_Helper import *
from live_api.tools.constant import *
import live_api.tools.utils_file as ufile
import os
import random
import time
import sys

reload(sys)
sys.setdefaultencoding("utf-8")


def env(key):
    if key in os.environ:
        return os.environ[key]
    else:
        return ""


# 获取某个位置的视频数
#addr_area addr_city  addr_province
# @csrf_exempt
# def addr_video_count(request):
#     ret = ReturnJ()
#     db =DB.getDB()
#     try:
#         info = INFO(request)
#         info.checkPostEmpty('province','city','area')
#         sql=get_area(info)
#         sql = "select count(*) as num from video where ctype=0 and id_area in (%s)"%sql
#         row_live = db.queryOne(sql)
#         sql = "select count(*) as num from video where ctype=1 and id_area in (%s)"%sql
#         row_hostry = db.queryOne(sql)
#         ret.province=info.province
#         ret.city=info.city
#         ret.area=info.area
#         ret.counter=str(row_live['num']+row_hostry['num'])
#         ret.isAliving="0" if row_live['num']==0 else 1
#         ret.msg = "成功"
#
#     except Exception, e:
#         ret.code = 1010
#         ret.msg = str(e)
#     return HttpResponse(ret.toJson())

# 获取某个位置的视频列表
@csrf_exempt
def addr_video_list(request):
    ret = ReturnJ()
    db =DB.getDB()
    try:
        info = INFO(request)
        info.checkPostEmpty('province','city','area')
        sql=get_area(info)
        sql = "select * from video where id_area in (%s)"%sql
        n,rows = db.query(sql)
        ret.province=info.province
        ret.city=info.city
        ret.area=info.area
        ret.videolist=out_farmat_video(rows,ctype=0)
        ret.msg = "成功"

    except Exception, e:
        ret.code = 1010
        ret.msg = str(e)
    return HttpResponse(ret.toJson())

# 获取某个位置的视频列表
@csrf_exempt
def get_area(info):
    sql_city=""
    sql_area=""
    if info.province!='':
        sql_province="select id from addr_province where province='%s'"%info.province
    else:
        raise Exception('addr_province(省份)不能为空')
    if info.city!='':
        sql_city="city='%s' and "%info.city
    if info.area!='':
        sql_area="area='%s' and "%info.area
    sql="select id from addr_area where %s id_city in( select id from addr_city where %s id_province in(%s))"%(sql_area,sql_city,sql_province)
    return sql


@csrf_exempt
def get_group(request):
    ret = ReturnJ()
    db = DB.getDB()
    info = INFO(request)
    info.checkPostEmpty('type')
    groups = {}

    try:
        n,rows = db.query("select * from video where par1=0")
        for row in rows:

            province = ''
            city = ''
            area = ''
            if info.type=='1':
                province = row['l_province']
                addressDetail = ''
            elif info.type=='2':
                province = row['l_province']
                city = row['l_city']
                addressDetail = ''
            else: #info.type==3:
                province = row['l_province']
                city = row['l_city']
                area = row['l_area']
                # print city,len(city)
                # print 'area',type(area)
                # print 'str(area)',str(area)
                area_str = "%s%s%s" % (province,city,area)
                # print area_str
                dict_address = {'陕西省西安市未央区':'西安市未央区龙首北路西段1号','陕西省西安市新城区':'西安市新城区尚德路115号','陕西省西安市莲湖区':'西安市莲湖区二府街20号','陕西省西安市碑林区':'西安市碑林区友谊西路72号','陕西省西安市灞桥区':'西安市灞桥区纺一路809号','陕西省西安市雁塔区':'西安市雁塔区含光路南段100号','陕西省西安市长安区':'西安市长安区西长安街669号','陕西省西安市鄠邑区':'西安市鄠邑区画展街9号','陕西省西安市临潼区':'西安市临潼区书院门街41号','陕西省西安市高陵区':'西安市高陵区县门街29号','陕西省西安市阎良区':'西安市阎良区延安街6号','陕西西安':'西安市碑林区测绘西路24号'}
                # print 111111111,area
                if dict_address.has_key(area_str):
                    addressDetail = dict_address[area_str]
                else:
                    addressDetail = ''
            l = "%s%s%s"%(province,city,area)
            # print 2222222,l
            isaliving = row['ctype']
            if not groups.has_key(l):
                groups[l] = {"province":province,"city":city,"area":area,"counter":1,"isaliving":isaliving,'addressDetail':addressDetail}
            else:
                groups[l]['counter']=groups[l]['counter']+1
                if isaliving==0:
                    groups[l]['isaliving']=isaliving

        data = []
        for group in groups.values():
            data.append(group)
        ret.data=data
    except Exception,e:
        ret.code = 1010
        ret.msg = str(e)
    return HttpResponse(ret.toJson())

@csrf_exempt
def get_video_list(request):
    ret = ReturnJ()
    db = DB.getDB()
    info = INFO(request)
    info.checkPostEmpty('province', 'city', 'area')
    # print info.province,info.city,info.area
    if info.area != '':
        sql = "select * from video where l_province='%s' and l_city='%s' and l_area='%s' and par1=0 order by ctype ASC ,start_date DESC "%(info.province,info.city,info.area)
    elif info.city != '':
        sql = "select * from video where l_province='%s' and l_city='%s' and par1=0 order by ctype ASC ,start_date DESC " % (info.province, info.city)
    else:
        sql = "select * from video where l_province='%s' and par1=0 order by ctype ASC ,start_date DESC " % (info.province)
    try:
        n,rows = db.query(sql)
        ret.videolist = out_farmat_video(rows, ctype=0)
        # print n,sql
    except Exception,e:
        ret.code = 1010
        ret.msg = str(e)
    return HttpResponse(ret.toJson())